package com.yunqiu.dbs;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;

import com.yunqiu.dbs.service.DBService;
import com.yunqiu.dbs.service.impl.MySqlDBService;
import com.yunqiu.dbs.service.impl.OracleDBService;
import com.yunqiu.dbs.service.impl.SqlServerDBService;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * java -jar dbservice.jar
 */
@SpringBootApplication
public class DbsApplication {

    /**
     * 默认数据库类型:mysql
     * mysql/oracle/sqlserver
     */
    private static String dbType = "mysql";
    private static String dbIp = "127.0.0.1";
    private static String dbPort = "3306";
    private static String dbName = "test";
    private static String dbUserName = "root";
    private static String dbPwd = "123456";

    @SneakyThrows
    public static void main(String[] args) {

		String dbTypeTemp = "";
		String dbIpTemp = "";
		String dbPortTemp = "";
		String dbNameTemp = "";
		String dbUserNameTemp = "";
		String dbPwdTemp = "";
    	if(Files.exists(Paths.get("db.properties"))){
			Properties properties = new Properties();
			properties.load(new FileInputStream("db.properties"));
			dbTypeTemp = properties.getProperty("dbType");
			dbIpTemp = properties.getProperty("dbIp");
			dbPortTemp = properties.getProperty("dbPort");
			dbNameTemp = properties.getProperty("dbName");
			dbUserNameTemp = properties.getProperty("dbUserName");
			dbPwdTemp = properties.getProperty("dbPwd");
		}

    	if(StringUtils.isAnyEmpty(dbTypeTemp,dbIpTemp,dbPortTemp,dbNameTemp,dbUserNameTemp,dbPwdTemp)){
			readToProperties();
		}else{
			byte[] buffer = new byte[1024];
			System.out.println("是否使用上次连接的数据库?y/n:" + new StringBuffer()
					.append("dbType:" + dbTypeTemp+"://"+dbIpTemp+":"+dbPortTemp+"/"+dbNameTemp+"@"+dbUserNameTemp)
					.toString());
			System.in.read(buffer);
			if(Objects.equals(getInput(buffer),"y")){
				dbType = dbTypeTemp;
				dbIp = dbIpTemp;
				dbPort = dbPortTemp;
				dbName = dbNameTemp;
				dbUserName = dbUserNameTemp;
				dbPwd = dbPwdTemp;
			}else{
				readToProperties();
			}
		}

        //执行服务
        DBService dbService = getDBService();
        dbService.execute();
    }

    @SneakyThrows
    private static void readToProperties() {
        byte[] buffer = new byte[1024];
        System.out.print("请选择数据库类型 1:mysql;2:oracle;3:sqlserver(默认mysql)>>");
        System.in.read(buffer);
        if ("2".equals(new String(buffer, "utf-8"))) {
            dbType = "oracle";
            dbPort = "1521";
        } else if ("3".equals(new String(buffer, "utf-8"))) {
            dbType = "sqlserver";
            dbPort = "1433";
        } else {
            dbType = "mysql";
            dbPort = "3306";
        }

        System.out.println("请输入数据库ip:port:db(like 127.0.0.1:3306:test 如只输入ip则用默认端口)>>");
        Arrays.fill(buffer, (byte) 0);
        System.in.read(buffer);
        if (StringUtils.isNotEmpty(getInput(buffer))) {
            String[] ipPort = getInput(buffer).split(":");
            dbIp = ipPort[0];
            if (ipPort.length > 1) {
                dbPort = ipPort[1];
            }
            if (ipPort.length > 2) {
                dbName = ipPort[2];
            }
        }

        System.out.println("请输入用户/密码信息(如:root:123456)>>");
        Arrays.fill(buffer, (byte) 0);
        System.in.read(buffer);
        if (StringUtils.isNotEmpty(getInput(buffer))) {
            String[] userPwd = getInput(buffer).split(":");
            dbUserName = userPwd[0];
            if (userPwd.length > 1) {
                dbPwd = userPwd[1];
            }
        }

        Properties properties = new Properties();
        properties.put("dbType", dbType);
        properties.put("dbIp", dbIp);
        properties.put("dbPort", dbPort);
        properties.put("dbName", dbName);
        properties.put("dbUserName", dbUserName);
        properties.put("dbPwd", dbPwd);
        properties.store(new FileWriter("db.properties"), "");
    }

    @SneakyThrows
    private static String getInput(byte[] buffer) {
        return new String(buffer, "utf-8").trim();
    }

    private static DBService getDBService() {
        if ("sqlserver".equalsIgnoreCase(dbType)) {
            return new SqlServerDBService(dbIp, dbPort, dbName, dbUserName, dbPwd);
        }

        if ("oracle".equalsIgnoreCase(dbType)) {
            return new OracleDBService(dbIp, dbPort, dbName, dbUserName, dbPwd);
        }

        return new MySqlDBService(dbIp, dbPort, dbName, dbUserName, dbPwd);
    }
}
